#include "form_setting_version.h"
#include "ui_form_setting_version.h"

// add necessary includes here
#include "utils/can/can_bll_t1c.h"
#include "utils/configure/configure_bll.h"

/*
 * Function:    FormSettingVersion
 * Description: The default constructor
 * Paramer:     void
 * Return:
 */
FormSettingVersion::FormSettingVersion(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormSettingVersion)
{
    qDebug() << Q_FUNC_INFO;

    ui->setupUi(this);
}

/*
 * FormHistoryResult:    ~FormSettingVersion
 * Description: The default destructor
 * Paramer:     void
 * Return:
 */
FormSettingVersion::~FormSettingVersion()
{
    qDebug() << Q_FUNC_INFO;

    delete ui;
}

/*
 * Function:    getInstance
 * Description: Get an instance of "FormSettingVersion" class, if exist, return it
 * Paramer:     QWidget *pParent - the pointer of QWidget class
 * Return:      the pointer of "FormSettingVersion" class
 */
FormSettingVersion* FormSettingVersion::getInstance(QWidget *pParent)
{
    static FormSettingVersion *pNew = NULL;
    if(!pNew) {
        qDebug() << Q_FUNC_INFO;

        // Create a new object if it doesn't exist
        pNew = new FormSettingVersion(pParent);
        if(NULL == pNew) return NULL;

        // After constructing object, initialize the object
        pNew->_Initialze();

        // Initialize control's UI
        pNew->_InitUi();

        // Hide widget firstly
        pNew->hide();
    }
    else {
        // Set the parent object
        pNew->setParent(pParent);
    }

    return pNew;
}

/*
 * Function:    _Initialze
 * Description: After constructing object, initialize the object
 * Paramer:     void
 * Return:      void
 */
void FormSettingVersion::_Initialze()
{
    qDebug() << Q_FUNC_INFO;
    std::vector<bool> vecConnect;
    vecConnect.push_back(QObject::connect(this, SIGNAL(signalRefreshUI()), this, SLOT(slotRefreshUI()), static_cast<Qt::ConnectionType>(Qt::AutoConnection | Qt::UniqueConnection)));
    for(int i=0; i<(int)vecConnect.size(); i++) {
        Q_ASSERT(vecConnect.at(i));
    }
}

/*
* Function:		_InitUi
* Description:	Init the window style
* Paramer:		void
* Return:		void
*/
void FormSettingVersion::_InitUi()
{
    qDebug() << Q_FUNC_INFO;

    // Displays the background image of the window
    QWidget::setAttribute(Qt::WA_StyledBackground);

    SET_STYLE_SHEET(QString(":/%1/setting/%2.qss").arg("skin_1").arg(this->objectName()));

    ui->label_Product->setWordWrap(true);

    // Display the company name
//    _showCompanyName();
}

/*
* Function:		on_pushButton_Home_clicked
* Description:	Return main window after click the button
* Paramer:		void
* Return:		void
*/
void FormSettingVersion::on_pushButton_Home_clicked()
{
    qDebug() << Q_FUNC_INFO;

    // Goto the home window
    emit signalShowHomeWindow();
}

/*
* Function:		on_pushButton_Setting_clicked
* Description:	Return the setting window after click the button
* Paramer:		void
* Return:		void
*/
void FormSettingVersion::on_pushButton_Setting_clicked()
{
    qDebug() << Q_FUNC_INFO;

    // Goto the main window
    emit signalShowSettingWindow();
}

/*
* Function:		slotRefreshUI
* Description:	The slot that refresh UI info
* Paramer:		void
* Return:		void
*/
void FormSettingVersion::slotRefreshUI()
{
    qDebug() << Q_FUNC_INFO;

    ui->label_InstrumentModel->setText(tr("Instrument model:"));
    ui->label_Version_APP->setText(tr("APP Version:"));
    ui->label_InstrumentID_Motion->setText(tr("Instrument ID(motion):"));
    ui->label_Version_IAP_Motion->setText(tr("IAP Version(motion):"));
    ui->label_InstrumentID_Pipette->setText(tr("Instrument ID(pipette):"));
    ui->label_Version_IAP_Pipette->setText(tr("IAP Version(pipette):"));
    ui->label_InstrumentID_Heating->setText(tr("Instrument ID(heating):"));
    ui->label_Version_IAP_Heating->setText(tr("IAP Version(heating):"));

    QString sBeta = "";
    QString sReg = "";

#ifndef DEF_PRODUCT_MODE
    sBeta = DEF_VER_BETA_TIMES;
#endif

#ifdef DEF_EXPECT_RESULT
    sReg = DEF_VER_REG_FLAG;
#endif

    // Set the instrument model info
    QString sInstrumentModel = QString("%1 %2")
            .arg(tr("Instrument model:"))
            .arg(DEF_DEVICE_MODEL);
    ui->label_InstrumentModel->setText(sInstrumentModel);

    // Set the app info
    QString sVerAPP = QString("%1 %2.%3%4%5")
            .arg(tr("APP Version:"))
            .arg(DEF_COMPLETE_VER)
            .arg(DEF_COMPLETE_VER_EX)
            .arg(sBeta)
            .arg(sReg);
    ui->label_Version_APP->setText(sVerAPP);

    QTimer::singleShot(100, this, [=](){
        // Get motion & pipette & heating module infomations
        QList<QString> lstInstrumentID = QList<QString>() << tr("Instrument ID(motion)") << tr("Instrument ID(pipette)") << tr("Instrument ID(heating)");
        QList<QString> lstVersionIAP = QList<QString>() << tr("IAP Version(motion)") << tr("IAP Version(pipette)") << tr("IAP Version(heating)");
        QList<unsigned int> lstCanID = QList<unsigned int>() << CanVars::gs_nIapCanID_CH1 << CanVars::gs_nPipetteCanID_CH1 << CanVars::gs_nHeatIapCanID_CH1;
        QList<QLabel *> lstLabelInstrumentID = QList<QLabel *>() << ui->label_InstrumentID_Motion << ui->label_InstrumentID_Pipette << ui->label_InstrumentID_Heating;
        QList<QLabel *> lstLabelVersionIAP = QList<QLabel *>() << ui->label_Version_IAP_Motion << ui->label_Version_IAP_Pipette << ui->label_Version_IAP_Heating;

        const int nListSize = 3;
        if(nListSize == lstInstrumentID.size() && nListSize == lstVersionIAP.size() && nListSize == lstCanID.size() && nListSize == lstLabelInstrumentID.size() && nListSize == lstLabelVersionIAP.size()) {
            for(int i=0; i<nListSize; i++) {
                STC_VERSION_INFO stVerInfo;
                int nErrCode = CanBLL_T1C::getInstance(this)->getIapVersion(stVerInfo, lstCanID.at(i));
                if(0 == nErrCode) {
                    QString sInstrumentID = QString("%1: %2").arg(lstInstrumentID.at(i)).arg(stVerInfo.sSerialNumber);
                    QString sVerIAP = QString("%1: %2").arg(lstVersionIAP.at(i)).arg(stVerInfo.sFirmwareVer);

                    sInstrumentID.replace("MDX", "");

                    lstLabelInstrumentID.at(i)->setText(sInstrumentID);
                    lstLabelVersionIAP.at(i)->setText(sVerIAP);
                }
            }
        }
    });

    // Get the login mode
    ST_CFG_LOGIN_MODE stLoginMode;
    ConfigureBLL::getInstance(this)->loadLoginMode(stLoginMode);

    ui->label_InstrumentID_Motion->setVisible(false);
    ui->label_Version_IAP_Motion->setVisible(false);
    ui->label_InstrumentID_Pipette->setVisible(false);
    ui->label_Version_IAP_Pipette->setVisible(false);
    ui->label_InstrumentID_Heating->setVisible(false);
    ui->label_Version_IAP_Heating->setVisible(false);

    switch ((int)stLoginMode.eumLoginMode) {
    case E_MODE_ADMIN:
    case E_MODE_FACTORY:
        ui->label_InstrumentID_Motion->setVisible(true);
        ui->label_Version_IAP_Motion->setVisible(true);
        ui->label_InstrumentID_Pipette->setVisible(true);
        ui->label_Version_IAP_Pipette->setVisible(true);
        ui->label_InstrumentID_Heating->setVisible(true);
        ui->label_Version_IAP_Heating->setVisible(true);
        break;
    default: break;
    }
}

/*
* Function:		changeEvent
* Description:	Triggers an event before it is switch language
* Paramer:		QShowEvent *event - the pointer of "QEvent"
* Return:		void
*/
void FormSettingVersion::changeEvent(QEvent *e)
{
    switch (e->type()) {
    case QEvent::LanguageChange:
    {
        ui->retranslateUi(this);

        // Display the company name
        _showCompanyName();

        qDebug() << Q_FUNC_INFO;
        break;
    }
    default:
        break;
    }

    QWidget::changeEvent(e);
}

/*
 * Function:    _showCompanyName
 * Description: Display the company name
 * Paramer:     void
 * Return:      void
 */
void FormSettingVersion::_showCompanyName()
{
    ui->label_Company->setText(tr("Guangzhou Ripnano Biology Co., Ltd"));

}
